package com.salim.leetcode.$68;

import java.util.ArrayList;
import java.util.List;

public class TextJustification {
    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> result = new ArrayList<>();
        int lineCount = 0;
        int wordsCount = 0;
        List<String> sub = new ArrayList<>();
        boolean flush = false;
        for(int i=0;i<words.length;i++){
            lineCount+=words[i].length();
            wordsCount+=words[i].length();
            if(lineCount==maxWidth){
                sub.add(words[i]);
                flush = true;
            }else if(lineCount<maxWidth){
                lineCount++;
                sub.add(words[i]);
            }else{
                lineCount-=words[i].length();
                wordsCount-=words[i].length();
                flush = true;
                i--;
            }

            if(flush){
                //最后一行 或只有一个单词 左对齐
                if(i+1==words.length || sub.size()==1){
                    left(maxWidth, result, sub);
                }
                //居中对齐
                else{
                    StringBuilder sb = new StringBuilder();

                    for(int j=0;j<sub.size()-1;j++){
                        if(j<(maxWidth-wordsCount)%(sub.size()-1)){
                            sb.append(sub.get(j));
                            for(int k=0;k< (maxWidth-wordsCount)/(sub.size()-1)+1;k++){
                                sb.append(" ");
                            }
                        }else{
                            sb.append(sub.get(j));
                            for(int k=0;k< (maxWidth-wordsCount)/(sub.size()-1);k++){
                                sb.append(" ");
                            }
                        }
                    }
                    sb.append(sub.get(sub.size()-1));
                    result.add(sb.toString());
                }

                sub = new ArrayList<>();
                lineCount = 0;
                wordsCount = 0;
                flush = false;
            }
        }
        if(sub.size()!=0){
            left(maxWidth, result, sub);
        }
        return result;
    }

    private void left(int maxWidth, List<String> result, List<String> sub) {
        StringBuilder sb = new StringBuilder();
        for(String str:sub){
            sb.append(str);
            sb.append(" ");
        }
        sb.deleteCharAt(sb.length()-1);
        int length = maxWidth-sb.length();
        for(int j=0;j<length;j++){
            sb.append(" ");
        }
        result.add(sb.toString());
    }

    public static void main(String[] args) {
        TextJustification f = new TextJustification();
//        a.addBinary("0","0");
        //f.fullJustify(new String[]{"This", "is", "an", "example", "of", "text", "justification."},16);
        f.fullJustify(new String[]{"What","must","be","acknowledgment","shall","be"},16);
        //f.fullJustify(new String[]{"The","important","thing","is","not","to","stop","questioning.","Curiosity","has","its","own","reason","for","existing."},16);
    }

}
